<?php


namespace app\erp\controller;


use app\erp\model\MenuAuthdb;

class MenuAuth extends Base
{
    protected $cols = array(
        array(
            'field' => 'id',
            'width' => 40,
            'title' => 'ID',
            'type' => 'checkbox',
            'fixed' => 'left'
        ),
        array(
            'field' => 'name',
            'width' => 300,
            'title' => '授权标题'
        ),
        array(
            'field' => 'vars',
            'width' => 240,
            'title' => '授权变量'
        ),
        array(
            'field' => 'member_ids',
            'width' => 400,
            'title' => '授权会员ID'
        ),
        array(
            'fixed' => 'right',
            'width' => 80,
            'title' => '操作',
            'toolbar' => '#menuAuth_tool'
        )
    );
    public $post = array();

    public function _initialize()
    {
        parent::_initialize();
        $request = request();
        $param = $request->param();
        $this->post = $param;
    }

    public function index()
    {
        $this->assign('cols', json_encode($this->cols));
        return $this->fetch();
    }

    public function ajaxList()
    {
        $menuAuthdb = new MenuAuthdb();
        $menuAuthdb->param = $this->post;
        $data = $menuAuthdb->order('id desc')->paginate($this->post['limit']);
        $this->layuiReturn($data->items(), $data->total());
    }

    public function tplForm()
    {
        $id = 0;
        $data = array();
        if (isset($this->post['id'])) {
            $menuAuthdb = new MenuAuthdb();
            $data = $menuAuthdb->find($this->post['id']);
            $id = $this->post['id'];
        }
        $this->assign('data', json_encode($data));
        $this->assign('id', $id);
        return $this->fetch();
    }

    public function saveData()
    {
        $menuAuthdb = new MenuAuthdb();
        if (isset($this->post['id']) && $this->post['id']) {
            $get_data = $menuAuthdb->find($this->post['id']);
            $get_data->save($this->post);
        } else {
            $menuAuthdb->save($this->post);
        }
        $this->layuiReturn('保存成功');
    }

    public function ajaxDelete()
    {
        $menuAuthdb = new MenuAuthdb();
        if (is_array($this->post['id'])) {
            MenuAuthdb::destroy($this->post['id']);
        } else {
            $data = $menuAuthdb->find($this->post['id']);
            $data->delete();
        }

        $this->layuiReturn('删除成功');
    }
}